package com.cqc.other.juc.synchronize.demo1;

import java.util.concurrent.TimeUnit;

/**
 * @author cui
 * @date 2025/6/25 22:16
 * @description 原文链接：https://pzblog.cn/java/concurrent/concurrent-synchronized.html
 * <p>
 * synchronized加在方法上，锁对象即使 synchronized 所在类的对象，
 * 如果是同一个对象，那么就是同一把锁，比如只创建了1个  DataEntity
 * 如果是不同的对象，那么就是不同的锁。比如创建了2个  DataEntity，就是2个锁，这2个锁不同，就互不影响。
 **/
public class SynchronizedTest {


    public static void main(String[] args) throws InterruptedException {
        DataEntity dataEntity = new DataEntity();
        DataEntity dataEntity2 = new DataEntity();

        AThread aThread = new AThread(dataEntity);
        BThread bThread = new BThread(dataEntity2);

        aThread.start();
        bThread.start();

        TimeUnit.SECONDS.sleep(1);
        System.out.println("result: " + dataEntity.getCount());
    }

    public static void main2(String[] args) throws InterruptedException {
        DataEntity dataEntity = new DataEntity();

        AThread aThread = new AThread(dataEntity);
        BThread bThread = new BThread(dataEntity);

        aThread.start();
        bThread.start();

        TimeUnit.SECONDS.sleep(1);
        System.out.println("result: " + dataEntity.getCount());
    }
}
